package com.senven.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senven.api.commom.ResponsePage;
import com.senven.api.dao.OrganUserMapper;
import com.senven.api.pojo.OrganAffair;
import com.senven.api.pojo.OrganUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Date;
import java.util.List;

/**
 * @author LQP
 */
@Service
public class OrganUserService {

    @Autowired
    private OrganUserMapper organUserMapper;

    /**
     * 获取所有社团成员
     * @param current
     * @param size
     * @return
     */
    public ResponsePage<OrganUser> queryOrganUserListAll(int current, int size){
        Page<OrganUser> page = new Page<OrganUser>(current, size);

        IPage<OrganUser> iPage = organUserMapper.selectPage(page,null);
        ResponsePage<OrganUser> responsePage= new ResponsePage<OrganUser>();
        responsePage.makePage(iPage);

        return responsePage;
    }


    /**
     * 根据ID获取所有社团成员
     * @param oid
     * @param current
     * @param size
     * @return
     */
    public ResponsePage<OrganUser> queryOrganUserList(int oid, int current, int size){
        Page<OrganUser> page = new Page<OrganUser>(current, size);
        QueryWrapper<OrganUser> wrapper = new QueryWrapper<OrganUser>();
        wrapper.eq("oid",oid);

        IPage<OrganUser> iPage = organUserMapper.selectPage(page,wrapper);
        ResponsePage<OrganUser> responsePage= new ResponsePage<OrganUser>();
        responsePage.makePage(iPage);

        return responsePage;
    }

    /**
     * 根据OID和姓名获取所有社团成员
     * @param oid
     * @param current
     * @param size
     * @return
     */
    public ResponsePage<OrganUser> queryOrganUserListByNameAndOid(int oid, String name, int current, int size){
        Page<OrganUser> page = new Page<OrganUser>(current, size);
        QueryWrapper<OrganUser> wrapper = new QueryWrapper<OrganUser>();
        wrapper.eq("oid",oid).eq("name",name);

        IPage<OrganUser> iPage = organUserMapper.selectPage(page,wrapper);
        ResponsePage<OrganUser> responsePage= new ResponsePage<OrganUser>();
        responsePage.makePage(iPage);
        return responsePage;
    }

    /**
     * 根据姓名获取所有社团成员
     * @param name
     * @param current
     * @param size
     * @return
     */
    public ResponsePage<OrganUser> queryOrganUserListByName(String name, int current, int size){
        Page<OrganUser> page = new Page<OrganUser>(current, size);
        QueryWrapper<OrganUser> wrapper = new QueryWrapper<OrganUser>();
        wrapper.like("uname",name);

        IPage<OrganUser> iPage = organUserMapper.selectPage(page,wrapper);
        ResponsePage<OrganUser> responsePage= new ResponsePage<OrganUser>();
        responsePage.makePage(iPage);

        return responsePage;
    }

    /**
     * 插入一个成员
     * @param organUser
     * @return
     */
    public int insertOrganUser(OrganUser organUser){
        organUser.setJoinTime(new Date());
        int result = organUserMapper.insert(organUser);
        return result;
    }

    /**
     * 删除一个成员
     * @param uid,oid
     * @return
     */
    public int deleteOrganUser(int oid, String uid){
        QueryWrapper<OrganUser> wrapper = new QueryWrapper<OrganUser>();
        wrapper.eq("uid",uid).eq("oid",oid);
        int result = organUserMapper.delete(wrapper);
        return result;
    }

    /**
     * 更新一个成员
     * @param organUser
     * @return
     */
    public int updateOrganUser(OrganUser organUser) {
        QueryWrapper<OrganUser> wrapper = new QueryWrapper<OrganUser>();
        wrapper.eq("uid",organUser.getUid()).eq("oid",organUser.getOid());
        int result = organUserMapper.update(organUser,wrapper);
        return result;
    }


    /**
     * 导出成员列表
     * @return
     */
    public List<OrganUser> exportMember(){
        List<OrganUser> result =  organUserMapper.selectList(null);
        return result;
    }
}
